<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <ul>

        <li>click me</li>

        <li>click me</li>

        <li>click me</li>

        <li>click me</li>

        <li>click me</li>

    </ul>
</div>

<script>

    var elements = document.getElementsByTagName('li');

    var length = elements.length;

    for (var i = 0; i < length; i++) {
        elements[i].onclick   = (function (i) {
            return function () {
                alert(i);
            };
        })(i);
    }


</script>

</body>
</html>